<?php
    require_once 'dataprovider.php';
    require_once '../dto/memberdto.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of membersdao
 *
 * @author Circle
 */
class membersdao {
    //put your code here
    public static function LoadAllMembers()
    {
        $sql='SELECT * FROM members ';
        $result = dataprovider::ExecuteQuery($sql);
        $arrMembers = new ArrayObject();
        while($row = mysql_fetch_array($result))
        {
            $memberdto = new memberdto();
            $memberdto->setUserID($row['UserID']);
            $memberdto->setPassword($row['Password']);
            $memberdto->setEmail($row['Email']);
            $memberdto->setAge($row['Age']);
            $memberdto->setStatus($row['Status']);
            $memberdto->setLocation($row['Location']);
            $memberdto->setGender($row['Gender']);
            $arrMembers[]=$memberdto;
        }
        return $arrMembers;
    }
    
    public static function GetMembersByID($id)
    {
        $sql="SELECT UserID, Email,Age, Status, Location,Gender".
                        " FROM members WHERE UserID ='".$id."';";
        $resultquery = dataprovider::ExecuteQuery($sql);
        if($resultquery == null)
            return null;
        $member = new memberdto();
        while ($row = mysql_fetch_array($resultquery))
        {
            $member->setUserID($row['UserID']);
            $member->setEmail($row['Email']);
            $member->setAge($row['Age']);
            $member->setLocation($row['Location']);
            $member->setGender($row['Gender']);
        }
       return $member;   
    }
    
    public static function  GetMemberByUsernameAndPassword($username,$password){
        $sql="SELECT UserID, Email,Age, Status, Location,Gender".
                        " FROM members WHERE UserID='".$username."' and Password='".$password."';";
        $resultquery = dataprovider::ExecuteQuery($sql);
        if($resultquery == null)
            return null;
        $member = new memberdto();
        while ($row = mysql_fetch_array($resultquery))
        {
            $member->setUserID($row['UserID']);
            $member->setEmail($row['Email']);
        }
       return $member;        
    }

    public static function InsertMember(memberdto $member)
    {
        $sql = "INSERT INTO members (UserID,Password,Email,Age,Status,Location,Gender) VALUES ('".$member->getUserID()."','".$member->getPassword()."','".$member->getEmail()."','".$member->getAge()."','".$member->getStatus()."','".$member->getLocation()."','".$member->getGender()."')";
        $result = dataprovider::ExecuteQuery($sql);
        if($result)
            return TRUE;
        else {
            return FALSE;
        }
    }
    
    private static function fetchResult($result)
    {
        $arrMembers = new ArrayObject();
        while($row = mysql_fetch_array($result))
        {
            $memberdto = new membersdto();
            $memberdto->UserID = $row['UserID'];
            $memberdto->Password =$row['Password'];
            $memberdto->Email = $row['Email'];
            $memberdto->Age = $row['Age'];
            $memberdto->Status = $row['Status'];
            $memberdto->Location=$row['Location'];
            $memberdto->Gender =$row['Gender'];
            $arrMembers[]=$memberdto;
        }
        return $arrMembers;
    }
    
    public static function UpdateMember(memberdto $member)
    {
        $sql = "UPDATE members" . 
                " SET Password = '" .$member->getPassword()."', Email = '" .$member->getEmail()."',".
                    " Age ='".$member->getAge()."', Status ='".$member->getStatus()."', ".
                    " Location ='".$member->getLocation()."', Gender ='".$member->getGender()."'".
                " WHERE UserID = '".$member->getUserID()."'";
        //echo $sql;
        $result = dataprovider::ExecuteQuery($sql);
        if($result)
            return TRUE;
        else {
            return FALSE;
        }
    }
}
?>
